package com.cykj.pos.service.impl;

import com.cykj.common.core.domain.AjaxResult;
import com.cykj.pos.domain.BizWithdrawalRecords;
import com.cykj.pos.domain.dto.TransactionDetailsDTO;
import com.cykj.pos.domain.dto.TranscationListDTO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.AizWithdrawalProfitMapper;
import com.cykj.pos.domain.AizWithdrawalProfit;
import com.cykj.pos.service.IAizWithdrawalProfitService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 提现分润Service业务层处理
 *
 * @author weijianbo
 * @date 2025-02-17
 */
@Service
public class AizWithdrawalProfitServiceImpl extends ServiceImpl<AizWithdrawalProfitMapper, AizWithdrawalProfit> implements IAizWithdrawalProfitService {

    @Resource
    private AizWithdrawalProfitMapper aizWithdrawalProfitMapper;

    @Override
    public List<AizWithdrawalProfit> queryList(AizWithdrawalProfit aizWithdrawalProfit) {
        LambdaQueryWrapper<AizWithdrawalProfit> lqw = Wrappers.lambdaQuery();
        if (aizWithdrawalProfit.getProfitUserId() != null){
            lqw.eq(AizWithdrawalProfit::getProfitUserId ,aizWithdrawalProfit.getProfitUserId());
        }
        if (StringUtils.isNotBlank(aizWithdrawalProfit.getProfitUserName())){
            lqw.like(AizWithdrawalProfit::getProfitUserName ,aizWithdrawalProfit.getProfitUserName());
        }
        if (aizWithdrawalProfit.getProfitRate() != null){
            lqw.eq(AizWithdrawalProfit::getProfitRate ,aizWithdrawalProfit.getProfitRate());
        }
        if (aizWithdrawalProfit.getProfitAmount() != null){
            lqw.eq(AizWithdrawalProfit::getProfitAmount ,aizWithdrawalProfit.getProfitAmount());
        }
        if (aizWithdrawalProfit.getProfitFee() != null){
            lqw.eq(AizWithdrawalProfit::getProfitFee ,aizWithdrawalProfit.getProfitFee());
        }
        if (aizWithdrawalProfit.getWithdrawalUserId() != null){
            lqw.eq(AizWithdrawalProfit::getWithdrawalUserId ,aizWithdrawalProfit.getWithdrawalUserId());
        }
        if (StringUtils.isNotBlank(aizWithdrawalProfit.getWithdrawalUserName())){
            lqw.like(AizWithdrawalProfit::getWithdrawalUserName ,aizWithdrawalProfit.getWithdrawalUserName());
        }
        if (aizWithdrawalProfit.getWithdrawalAmount() != null){
            lqw.eq(AizWithdrawalProfit::getWithdrawalAmount ,aizWithdrawalProfit.getWithdrawalAmount());
        }
        if (aizWithdrawalProfit.getWithdrawalRate() != null){
            lqw.eq(AizWithdrawalProfit::getWithdrawalRate ,aizWithdrawalProfit.getWithdrawalRate());
        }
        if (StringUtils.isNotBlank(aizWithdrawalProfit.getVar1())){
            lqw.eq(AizWithdrawalProfit::getVar1 ,aizWithdrawalProfit.getVar1());
        }
        if (StringUtils.isNotBlank(aizWithdrawalProfit.getVar2())){
            lqw.eq(AizWithdrawalProfit::getVar2 ,aizWithdrawalProfit.getVar2());
        }
        if (StringUtils.isNotBlank(aizWithdrawalProfit.getVar3())){
            lqw.eq(AizWithdrawalProfit::getVar3 ,aizWithdrawalProfit.getVar3());
        }
        return this.list(lqw);
    }



//    @Override
//    public AjaxResult appBackWithdrawalProfitRecords(AizWithdrawalProfit aizWithdrawalProfit) {
//         List<AizWithdrawalProfit> aizWithdrawalProfitList =
//                 aizWithdrawalProfitMapper.selectAppBackWithdrawalProfitRecords(aizWithdrawalProfit);
//         return AjaxResult.success("提现分润记录", aizWithdrawalProfitList);
//    }

    @Override
    public AjaxResult appBackWithdrawalProfitRecords(TransactionDetailsDTO detailsDTO) {
        AizWithdrawalProfit aizWithdrawalProfit = new AizWithdrawalProfit();
        Long userId = detailsDTO.getUserId();
        aizWithdrawalProfit.setPageNo((detailsDTO.getPageNo()-1)*detailsDTO.getPageSize());
        aizWithdrawalProfit.setPageSize(detailsDTO.getPageSize());
        aizWithdrawalProfit.setProfitUserId(userId);
        if(detailsDTO.getCupsName() != null){
            aizWithdrawalProfit.setWithdrawalUserName(detailsDTO.getCupsName());
        }
        aizWithdrawalProfit.setStartTime(detailsDTO.getStartTime());
        aizWithdrawalProfit.setEndTime(detailsDTO.getEndTime());
        List<TranscationListDTO> aizWithdrawalProfitList =
                aizWithdrawalProfitMapper.selectAppBackWithdrawalProfitRecords(aizWithdrawalProfit);
        detailsDTO.setNumber(aizWithdrawalProfitList.size());
        detailsDTO.setProfitSharingAmount(aizWithdrawalProfitList.stream().mapToDouble(TranscationListDTO::getProfitSharingAmount).sum());
        detailsDTO.setTransactionAmount(aizWithdrawalProfitList.stream().mapToDouble(TranscationListDTO::getTransactionAmount).sum());
        detailsDTO.setDtolist(aizWithdrawalProfitList);
        return AjaxResult.success("提现记录",detailsDTO);
    }

}
